/**********************************************************************
 *** *                                                            * ***
 *** *  Copyright (c) 2013 Harmonia Holdings Group LLC            * ***
 *** *                                                            * ***
 *** * Permission is hereby granted to use, modify, copy, and     * ***
 *** * distribute this software and its documentation for any     * ***
 *** * purpose only without profit, provided that the above       * ***
 *** * Copyright Notice appears in all copies and that both the   * ***
 *** * Copyright Notice and this Permission Notice appears in     * ***
 *** * every copy of supporting documentation.  No title to nor   * ***
 *** * ownership of the software is transferred hereby.  The name * ***
 *** * of Harmonia Holdings Group LLC shall not be used in        * ***
 *** * advertising or publicity related to the distribution of    * ***
 *** * the software without specific, written, prior permission.  * ***
 *** * This software is provided as-delivered without expressed   * ***
 *** * or implied warranty.  Harmonia Holdings Group LLC          * ***
 *** * makes no representation about the suitability of this      * ***
 *** * software for any purpose and accepts no responsibility for * ***
 *** * its use.                                                   * ***
 *** *                                                            * ***
 *** ************************************************************** ***
 *** *                                                            * ***
 *** * This program is free software; you can redistribute it     * ***
 *** * and/or modify it under the terms of the GNU General Public * ***
 *** * License as published by the Free Software Foundation;      * ***
 *** * either version 2 of the License, or (at your option) any   * ***
 *** * later version.                                             * ***
 *** *                                                            * ***
 *** * This program is distributed in the hope that it will be    * ***
 *** * useful, but WITHOUT ANY WARRANTY; without even the implied * ***
 *** * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR    * ***
 *** * PURPOSE.  See the GNU General Public License for more      * ***
 *** * details.                                                   * ***
 *** *                                                            * ***
 *** * You should have received a copy of the GNU General Public  * ***
 *** * License along with this program; if not, write to the Free * ***
 *** * Software Foundation, Inc., 51 Franklin Street, Fifth       * ***
 *** * Floor, Boston, MA 02110-1301, USA.                         * ***
 *** *                                                            * ***
 *** * For more information: http://www.gnu.org/licenses/gpl.html * ***
 *** *                                                            * ***
 **********************************************************************/
package org.etexascode.interrep.datamodel.utils;

import java.awt.Polygon;

/**
 * UtilsSpecialEquals is a utility class for specialty equality operators.
 * 
 * @author ablatt
 */
public class UtilsSpecialEquals {
	
	/**
	 * Tests equality of 2 polygons specifically examining if the points which make up those 2 polygons are the same.
	 * @param p1 A polygon.
	 * @param p2 A polygon.
	 * @return The points in p1 are the same as the points in p2.
	 */
	public static boolean equals(Polygon p1, Polygon p2) {
		if(p1.npoints != p2.npoints) {
			return false;
		} else if(p1.npoints == 0) {
			return true;
		} else {
			int offset = 0;
			
			for(; offset < p1.npoints; offset++) {
				if(pointEquals(p1.xpoints[offset], p1.ypoints[offset], p2.xpoints[0], p2.ypoints[0])) {
					break;
				}
			}
			
			if(offset == p1.npoints) {
				return false;
			} else {
				boolean wasNotFound = false;
				for(int i = 0; i < p1.npoints; i++) {
					if(!pointEquals(p1.xpoints[(i + offset) % p1.npoints],
									p1.ypoints[(i + offset) % p1.npoints],
									p2.xpoints[i], p2.ypoints[i])) {
						wasNotFound = true;
					}
				}
				
				if(wasNotFound) {
					for(int i = 0; i < p1.npoints; i++) {
						if(!pointEquals(p1.xpoints[(offset + i + 1) % p1.npoints],
										p1.ypoints[(offset + i + 1) % p1.npoints],
										p2.xpoints[p1.npoints - i - 1], p2.ypoints[p1.npoints - i - 1])) {
							return false;
						}
					}
				}
				
				return true;
			}
		}
	}
	
	/**
	 * Is point (x1, y1) the same as the point (x2, y2)?
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @return If the points are equals.
	 */
	public static boolean pointEquals(int x1, int y1, int x2, int y2) {
		return (x1 == x2) && (y1 == y2);
	}
}
